<template>
	<view class="rank" ref="rank">
		<scroll :data="topList" class="toplist" ref="toplist">
			<ul v-if="topList.length">
				<li @click="selectItem(item)" class="item" v-for="(item, pIndex) in topList" :key="pIndex">
					<div class="icon">
						<img width="100" height="100" v-lazy="item.picUrl" />
					</div>
					<ul class="songlist">
						<li class="song" v-for="(song,index) in item.songList" :key="index">
							<span>{{index + 1}}</span>
							<span>{{song.songname}}-{{song.singername}}</span>
						</li>
					</ul>
				</li>
			</ul>
			<div class="loading-container" v-else>
				<loading title="加载中..." align="vertical" color="#ddd"></loading>
			</div>
		</scroll>
	</view>
</template>

<script>
	import Scroll from '@/components/scroll/scroll.vue'
	import Loading from '@/components/loading/loading.vue'
	import {getTopList} from '../../api/rank'
	import {playlistMixin} from '@/common/js/mixin'
	import {mapMutations} from 'vuex'

	export default {
		mixins: [playlistMixin],
		created() {
			this._getTopList()
		},
		data() {
			return {
				topList: []
			}
		},
		methods: {
			handlePlaylist(playlist) {
				const bottom = playlist.length > 0 ? '60px' : ''
				this.$refs.rank.$el.style.bottom = bottom
				this.$refs.toplist.refresh()
			},
			selectItem(item) {
				//console.log(item)
				uni.navigateTo({
					url:'/pages/rank/rankDetail'
				})
				this.setTopList(item)
			},
			_getTopList() {
				getTopList().then((res) => {
					if (res.code === this.ERR_OK) {
						this.topList = res.data.topList
					}
				})
			},
			...mapMutations({
				setTopList: 'setTopList'
			})
		},
		watch: {
			topList() {
				setTimeout(() => {
					this.$Lazyload.lazyLoadHandler()
				}, 20)
			}
		},
		components: {
			Scroll,
			Loading
		}
	}
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
  @import "../../../common/stylus/variable"	
  .rank
    position: fixed
    width: 100%
    top: 0px
    bottom: 0
    .toplist
      height: 100%
      overflow: hidden
      background: $color-background
      .item
        display: flex
        margin: 0 20px
        padding-top: 20px
        height: 100px
        &:last-child
          padding-bottom: 20px
        .icon
          flex: 0 0 100px
          width: 100px
          height: 100px
        .songlist
          flex: 1
          display: flex
          flex-direction: column
          justify-content: center
          padding: 0 20px
          height: 100px
          overflow: hidden
          background: $color-highlight-background
          color: $color-text-d
          font-size: $font-size-small
          .song
            no-wrap()
            line-height: 26px
      .loading-container
        position: absolute
        width: 100%
        top: 50%
        transform: translateY(-50%)
</style>
